package com.dwyinyuan.consumer;

import com.dwyinyuan.common.DownloadTask;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;

public class Consumer implements Runnable {

    private String name;
    private BlockingQueue<String> blockingQueue;
    private ExecutorService taskPool;

    public Consumer(String name, BlockingQueue<String> blockingQueue, ExecutorService taskPool) {
        this.name = name;
        this.blockingQueue = blockingQueue;
        this.taskPool = taskPool;
    }

    @Override
    public void run() {
        try {
            String src = null;
            while ((src = blockingQueue.take()) != "") {
                System.out.println(name + " take " + src);
                taskPool.submit(new DownloadTask(src));
            }
            System.out.println(name + " is over");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
